package zzzhc.image.model;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import zzzhc.image.recognize.Filter;
import zzzhc.image.recognize.ImageToken;
import zzzhc.image.recognize.RecognizeContext;
import zzzhc.image.recognize.nn.NNInput;
import zzzhc.image.recognize.nn.NNInputItem;

public class NNInputTableModel extends NormalTableModel {

	private static final long serialVersionUID = -8252683853318255695L;

	private static final String[] COLUMNS = {"来源", "分割结果", "对应字符", "输入参数" };

	private NNInput nnInput;

	public NNInputTableModel() {
		init(COLUMNS, new Class[] {String.class, BufferedImage.class, String.class,
				String.class }, new Boolean[] { false,false, false, false });
	}

	public void reload() {
		Filter filter = AppModel.getInstance().getOpPanelModel()
				.getFilterManager();
		CaptureTableModel ctm = AppModel.getInstance().getCapturePanelModel()
				.getCaptureTableModel();
		reload(filter, ctm);
	}

	public void reload(Filter filter, CaptureTableModel ctm) {
		clearAll();

		List<BufferedImage> list = ctm.getImages();

		int index = 0;

		Map<Character, Set<ImageToken>> map = new HashMap<Character, Set<ImageToken>>();
		for (BufferedImage image : list) {
			RecognizeContext source = new RecognizeContext(image);
			filter.doFilter(source);
			ImageToken[] tokens = source.getTokens().toArray(new ImageToken[0]);
			String code = ctm.getCode(index++);
			if (tokens.length != code.length()) {
				continue;
			}
			for (int i = 0; i < code.length(); i++) {
				char c = code.charAt(i);
				tokens[i].sourceStr = code;
				tokens[i].index = i;
				tokens[i].code = c;
				Set<ImageToken> tokenSet = map.get(c);
				if (tokenSet == null) {
					tokenSet = new HashSet<ImageToken>();
				}
				tokenSet.add(tokens[i]);
				map.put(c, tokenSet);
			}
		}

		nnInput = new NNInput();
		List<Character> keyList = new ArrayList<Character>(map.keySet());
		Collections.sort(keyList);

		for (Character c : keyList) {
			Set<ImageToken> tokenSet = map.get(c);
			List<ImageToken> tokenList = new ArrayList<ImageToken>(tokenSet);
			int end = tokenSet.size();// TODO
			for (int i = 0; i < end; i++) {
				NNInputItem item = NNInputItem.fromImageToken(tokenList.get(i),
						c);
				nnInput.addItem(item);
			}
		}
		// System.out.println(nnInput);
		addColumns(nnInput);
	}

	public void removeRow(int rowIndex) {
		super.removeRow(rowIndex);
		nnInput.getItems().remove(rowIndex);
	}

	public NNInput getNnInput() {
		return nnInput;
	}

	public void addColumns(NNInput input) {
		for (NNInputItem item : input.getItems()) {
			addColumn(item);
		}
	}

	public void addColumn(NNInputItem item) {
		Map<String, Object> map = new HashMap<String, Object>();
		ImageToken token = item.getImageToken();
		if (token != null) {
			map.put(COLUMNS[0], token.sourceStr+"("+token.index+")");
			map.put(COLUMNS[1], token.toImage());
		}
		map.put(COLUMNS[2], item.getCode());
		map.put(COLUMNS[3], item.getDataString());
		addRow(map);
	}
}
